package part10;

/**
 * @author wcj
 * @Date 2021/5/19 16:54
 * @Version 1.0
 */
public class Part102 {

    private Object[] items;

    private int next = 0;

    public Part102(int size) {
        items = new Object[size];
    }

    public void add(Object x) {
        if (next < items.length) {
            items[next++] = x;
        }
    }

    /**
     * 内部类具有外部类的所有成员的访问权限，因为外围类中创建内部类对象时，内部类中捕获了一个外围类的引用
     */
    private class SequenceSelector implements Selector {
        private int i = 0;

        @Override
        public boolean end() {
            return i == items.length;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if (i < items.length) {
                i++;
            }
        }
    }

    public Selector selector() {
        return new SequenceSelector();
    }

    public static void main(String[] args) {
        Part102 part102 = new Part102(10);
        for (int i = 0; i < 10; i++) {
            part102.add(Integer.toString(i));
        }
        Selector selector = part102.selector();
        while (!selector.end()) {
            System.out.println("current:" + selector.current());
            selector.next();
        }
    }
}

interface Selector {

    /**
     * end
     *
     * @return
     */
    boolean end();

    /**
     * current
     *
     * @return
     */
    Object current();

    /**
     * next
     */
    void next();
}
